'use strict';

const Controller = require('egg').Controller;

class RecommendController extends Controller {
  // 获取衣服推荐
  async clothing() {
    const { ctx } = this;
    const userId = ctx.state.user?.id || 1;

    try {
      // 简单的推荐逻辑：推荐穿着次数少的衣服
      const recommendations = await ctx.model.Clothing.findAll({
        where: { 
          user_id: userId,
          status: 'available',
        },
        order: [
          ['wear_count', 'ASC'],
          ['created_at', 'DESC'],
        ],
        limit: 10,
        include: [
          {
            model: ctx.model.Category,
            as: 'category',
            attributes: ['id', 'name'],
          },
        ],
      });

      ctx.body = {
        code: 200,
        message: '获取成功',
        data: recommendations,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 获取搭配推荐
  async outfit() {
    const { ctx } = this;
    const userId = ctx.state.user?.id || 1;

    try {
      // 简单的推荐逻辑：推荐评分高的搭配
      const recommendations = await ctx.model.Outfit.findAll({
        where: { 
          user_id: userId,
        },
        order: [
          ['rating', 'DESC'],
          ['wear_count', 'ASC'],
        ],
        limit: 10,
        include: [
          {
            model: ctx.model.Clothing,
            as: 'clothes',
            attributes: ['id', 'name', 'main_image'],
            through: { attributes: [] },
          },
        ],
      });

      ctx.body = {
        code: 200,
        message: '获取成功',
        data: recommendations,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // AI智能搭配
  async aiMatch() {
    const { ctx } = this;
    const userId = ctx.state.user?.id || 1;

    try {
      // 这里可以集成AI服务，暂时返回模拟数据
      const aiRecommendations = {
        weather_based: [],
        color_matching: [],
        style_suggestions: [],
      };

      ctx.body = {
        code: 200,
        message: 'AI推荐功能开发中',
        data: aiRecommendations,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }
}

module.exports = RecommendController;
